// pages/mine/anchorWelfare/anchorWelfare.js
const app = getApp();
const time = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //主要信息
    balance: [],
    code: '',
    extract_time: 0,
    receive_time: 0,

    //参数
    withdraw: false,
    canvas_bg: '',
    rpx: '',
    share_img: '',
    no_resume: 0,
    page: 1,

    zfb_account: '',
    apply_user: '',
    apply_amount: '',

    //弹框
    flag_rule: true,
    flag_canvas: true,
    flag_share: true,
    flag_ruleTwo: true,

    flag_update:false,

    //多次点击

  },
  canvas_hide(){
    this.setData({
      flag_canvas:true,
    })
  },
  // 是否要升级
  get_isupdate() {
    wx.request({
      url: app.globalData.url + 'api/broker/hand/upgrade/alert',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        let updateInfo = e.data.data,
          flag_update = e.data.data.is_upgrade == 1 ? true : false;
        this.setData({
          updateInfo: updateInfo,
          flag_update: flag_update,
        })

      },
    })
  },
  //去升级
  go_update() {
    this.get_isupdate();
    wx.navigateTo({
      url: '../anchorWelfare/upgrade/upgrade'
    })
  },
  //去规则页
  go_rule(){
    wx.navigateTo({
      url: 'rule/rule'
    })
  },
  getRule() {
    wx.request({
      url: app.globalData.url + 'api/broker/lift/rule',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        let welfare = e.data.data.welfare,
          rule = e.data.data.rule,
          upgrade = e.data.data;
        this.setData({
          upgrade: upgrade,
        })
      },

    })
  },
  //去升级
  go_upgrade() {
    wx.navigateTo({
      url: 'upgrade/upgrade',
    })
  },
  //查看公司
  go_check() {
    wx.navigateTo({
      url: 'check/check',
    })
  },
  //点击选项
  click_all(e) {
    // console.log(e)
    let num = e.currentTarget.dataset.num;
    if (num == 1) {
      this.setData({
        no_resume: 0,
        page: 1,
      })
      this.get_invite(0, 1);
    } else if (num == 2) {
      this.setData({
        no_resume: 1,
        page: 1,
      })
      this.get_invite(1, 1);
    }
  },


  //滚动加载
  loadDetail: function (e) {
    let that = this;
    let page = this.data.page;
    page = page + 1;
    this.setData({
      page: page
    })
    this.List_invite();
  },

  // 小播推荐
  List_invite: function (e) {
    let that = this;
    let page = this.data.page,
      no_resume = this.data.no_resume;
    that.get_invite(no_resume, page);
  },

  //邀请记录
  get_invite(no_resume, page) {
    wx.request({
      url: app.globalData.url + 'api/invitation/my/invite',
      data: {
        token: app.globalData.token,
        no_resume: no_resume,
        page: page
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
           if (e.data.data.current_page > e.data.data.total_page) {
          // console.log("到底了");
          let invite = e.data.data;
          this.setData({
            invite: invite,
          })
          // wx.showToast({
          //   title: '到底啦~',
          //   icon: 'none',
          // })
        } else {
          if (this.data.page == 1) {
            let bills = e.data.data.bills;
            let invite = e.data.data;
            bills.forEach(function (value, index) {
              value.create_time = time.formatTimeTwo(value.create_time, 'Y-M-D h:m');
            })
            this.setData({
              bills: bills,
              invite: invite,
            })
          } else {
            // console.log("不不不不不");
            let bills = that.data.bills;
            let invite = e.data.data;
            let _bills = e.data.data.bills;
            _bills.forEach(function (value, index) {
              value.create_time = time.formatTimeTwo(value.create_time, 'Y-M-D h:m');
            })
            bills.push(..._bills);
            //   console.log(CollectList);
            this.setData({
              invite: invite,
              bills: bills,
            })
          }
          wx.hideLoading();
        }
      },
    })
  },

  //隐藏弹框
  modal_hide() {
    this.setData({
      flag_share: true,
      // 升级
      flag_update: false,
    })
  },
  //加载个人信息
  getInfo() {
    wx.request({
      url: app.globalData.url + 'api/common/register/info',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        let info = e.data.data,
          realMoney = parseInt(info.bb) ? parseInt(info.bb) / this.data.balance.bb_rate : 0;
        this.setData({
          info: info,
          realMoney: realMoney,
        })
      },
    })
  },
  //分享链接
  click_url() {
    let that = this;
    let balance = that.data.balance;
    let share_url = balance.share_url;
    let code = balance.code;
    wx.showModal({
      content: '已复制，快去分享给好友吧！',
      showCancel: false,
      confirmText: '好的',
      confirmColor: '#d0a76c',
      success: function (e) {
        // console.log(e)
        if (e.confirm) {
          wx.setClipboardData({
            data: share_url + code,
          })
          that.setData({
            flag_share: true,
          })
        }
      },
    })
  },
  //分享图片
  click_img() {
    this.setData({
      flag_share: true,
      flag_canvas: false,
    })
  },
  //查看体现详情
  go_detail() {
    let that = this;
    wx.navigateTo({
      url: '../welfareDetail/welfareDetail',
    })
  },
  //邀请
  click_invite() {
    let that = this;
    wx.showLoading({
      mask: false,
    })
    var timer = setTimeout(function () {
      wx.hideLoading()
      clearTimeout(timer)
    }, 1000)
    that.balance();
    that.createCanvas();
    let balance = this.data.balance;
    if (!balance.code) {
      that.create_code();
      that.balance();
      that.setData({
        flag_share: false,
      })
      // wx.showToast({
      //   title: '请先生成邀请码',
      //   icon: 'none',
      // })
    }
    //  else if (balance.has_resume == "0") {
    //   wx.showToast({
    //     title: '请先上传简历',
    //     icon: 'none',
    //   })
    // }
    else {
      that.balance();
      that.setData({
        flag_share: false,
      })
    }

  },
  //保存图片
  click_save(e) {
    let that = this;
    // console.log(e)
    wx.getSetting({
      success(res) {
        // 如果没有则获取授权
        if (!res.authSetting['scope.writePhotosAlbum']) {

          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              wx.saveImageToPhotosAlbum({
                filePath: that.data.share_img,
                success() {
                  wx.showToast({
                    title: '保存成功',
                    icon: 'none',
                    success() {
                      that.setData({
                        flag_canvas: true,
                      })
                    }
                  })
                },
                fail() {
                  wx.showToast({
                    title: '保存失败',
                    icon: 'none',
                    success() {
                      that.setData({
                        flag_canvas: true,
                      })
                    }
                  })
                }
              })
            },
          })
        } else {
          // 有则直接保存
          that.setData({
            flag_canvas: true,
          })
          wx.saveImageToPhotosAlbum({
            filePath: that.data.share_img,
            success() {
              wx.showToast({
                title: '保存成功',
                icon: 'none',
                success() {
                  that.setData({
                    flag_canvas: true,
                  })
                }
              })
            },
            fail() {
              wx.showToast({
                title: '保存失败',
                icon: 'none',
                success() {
                  that.setData({
                    flag_canvas: true,
                  })
                }
              })
            }
          })
        }
      }
    })
  },
  //显示规则
  click_showrule() {
    this.setData({
      flag_rule: false,
    })
  },
  //隐藏
  click_hide() {
    this.setData({
      flag_rule: true,
      flag_ruleTwo: true,
      flag_update:false,
    })
  },
  //点击提现
  click_extract() {
    let that = this,
      zfb_account = this.data.zfb_account,
      apply_user = this.data.apply_user,
      apply_amount = parseInt(this.data.apply_amount),
      balance = this.data.balance,
      money = parseInt(balance.balance);

    if (money >= 7) {
      if (zfb_account != '' && apply_user != '' && apply_amount != '') {
        // console.log('apply_amount+' + apply_amount)
        // console.log('balance+' + money)
        if (apply_amount > money) {
          wx.showToast({
            title: '提现金额需小于可提现金额',
            icon: 'none',
          })
        } else if (apply_amount < 7) {
          wx.showToast({
            title: '提现金额需大于7元',
            icon: 'none',
          })
        } else if (7 <= apply_amount <= money) {
          let extract_time = that.data.extract_time;
          extract_time = extract_time + 1;
          that.setData({
            extract_time: extract_time
          })
          if (that.data.extract_time == 0) {
            this.extract(zfb_account, apply_user, apply_amount);
          }

        } else {
          console.log('none')
        }

      } else {
        wx.showToast({
          title: '请填写所有信息',
          icon: 'none',
        })
      }
    } else {
      wx.showToast({
        title: '满7元才能提现',
        icon: 'none',
      })
    }

  },
  click_back() {
    this.setData({
      zfb_account: '',
      apply_user: '',
      apply_amount: '',
      withdraw: false,
    })
  },
  //点击切换 提现
  change_withdraw() {
    wx.request({
      url: app.globalData.url + 'api/invitation/apply/condition',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        let type = e.data.data.type;
        this.setData({
          type: type,
        })
        if (e.data.data.type == 0) {
          wx.navigateTo({
            url: 'anchorWelfare_detail/anchorWelfare_detail',
          })
        } else if ((e.data.data.type == 2)) {
          wx.showToast({
            title: '简历审核中',
            icon: 'none',
          })
        } else if ((e.data.data.type == 7)) {
          wx.showToast({
            title: '提现审核中',
            icon: 'none',
          })
        } else {
          this.setData({
            flag_ruleTwo: false,
          })
        }
      },
    })
  },
  //账户
  inp_account(e) {
    // console.log(e.detail.value)
    let zfb_account = e.detail.value;
    this.setData({
      zfb_account: zfb_account,
    })
  },
  //姓名
  inp_name(e) {
    // console.log(e.detail.value)
    let apply_user = e.detail.value;
    this.setData({
      apply_user: apply_user,
    })
  },
  //金额
  inp_money(e) {
    // console.log(e.detail.value)
    let apply_amount = e.detail.value;
    this.setData({
      apply_amount: apply_amount,
    })
  },
  //生成邀请码
  create_code() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/invitation/create/code',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function (e) {
        // console.log(e);
        that.balance();
      },
    }),
      that.balance();
  },
  //复制
  click_copy() {
    let code = this.data.balance.code;
    wx.setClipboardData({
      data: code,
    })
  },
  //七日收益
  earns() {
    wx.request({
      url: app.globalData.url + 'api/task/seven/day/earnings',
      data: {
        token: app.globalData.token
      },
      method: 'POST',
      success: (e) => {
        let earnsInfo = e.data.data.earnings;
        var xInfo = [],
          yInfo = [];
        earnsInfo.forEach(function (value, index) {
          var str = value.show_date;
          var str_new = str.replace(/"-"/, ".");
          var str_re = str_new.substring(5);
          value.show_date = str_re;
          value.operate_num = parseInt(value.operate_num);
          xInfo.push(value.show_date);
          yInfo.push(value.operate_num)
        })
        // outSide.xInfo = xInfo;
        // outSide.yInfo = yInfo;
        wx.setStorageSync('xInfo', xInfo)
        wx.setStorageSync('yInfo', yInfo)

        // console.log(outSide.xInfo)
        // console.log(outSide.yInfo)
      }
    })
  },
  //提现
  extract(zfb_account, apply_user, apply_amount) {
    this.earns();
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/invitation/user/price',
      data: {
        token: app.globalData.token,
        zfb_account: zfb_account,
        apply_user: apply_user,
        apply_amount: apply_amount,
      },
      method: 'POST',
      success: function (e) {
        // console.log(e)
        // wx.showToast({
        //   title: '请等待,已提交申请!',
        //   icon: 'none',
        // })
        wx.showModal({
          content: '请等待,已提交申请!',
          showCancel: false,
          confirmText: '好的',
          confirmColor: '#d0a76c',
          success: function (e) {
            if (e.confirm) {
              wx.redirectTo({
                url: '../anchorWelfare/anchorWelfareRe',
                // url: '../../boMoney/index',
              })
            }
          },
        })
        that.balance();
      },
    })
  },
  //领取
  go_receive() {
    var that = this;
    var balance = this.data.balance;
    if (balance.has_resume == "2") {
      wx.showToast({
        title: '简历审核中，暂不能操作',
        icon: 'none',
      })
    } else if (balance.has_resume == "3") {
      wx.showToast({
        title: '简历审核失败，请重新上传',
        icon: 'none',
      })
    } else if (balance.has_resume == "1") {
      var receive_time = this.data.receive_time;
      receive_time = receive_time + 1;
      this.setData({
        receive_time: receive_time
      })
      this.balance();
      if (this.data.receive_time == 1) {
        wx.request({
          url: app.globalData.url + 'api/invitation/user/receive',
          data: {
            token: app.globalData.token,
          },
          method: 'POST',
          success: function (e) {
            // console.log(e)
            this.balance();
          },
        })
      }
    }

  },
  //去简历
  go_resume() {
    let that = this,
      balance = this.data.balance;
    if (balance.has_resume == 0) {
      wx.navigateTo({
        url: '../../resume/uploadResume/uploadResume?welfare=true',
      })
    } else if (balance.has_resume == 2) {
      wx.showToast({
        title: '简历正在审核中',
        icon: 'none',
      })
    } else if (balance.has_resume == 3) {
      wx.showModal({
        content: '审核失败，请重新发布',
        showCancel: true,
        confirmText: '重新发布',
        confirmColor: '#d0a76c',
        success: function (e) {
          if (e.confirm) {
            wx.navigateTo({
              url: '../../resume/uploadResume/uploadResume?welfare=true',
            })
          }
        },
      })
    }

  },
  //获取我的额度 等信息
  balance() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/invitation/my/balance',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function (e) {
        // console.log(e)
        let balance = e.data.data;
        let code = e.data.data.code,
          canvas_url = e.data.data.invitation_img_url,
          qrcode_url = e.data.data.qrcode_url;
        // console.log(canvas_url)
        // console.log(qrcode_url)
        that.setData({
          balance: balance,
          code: code,
          canvas_url: canvas_url,
          qrcode_url: qrcode_url,
        })
        that.download_qrcode_img();
        that.download_canvas_bg();
      },
    })
  },
  //downqrcode
  download_qrcode_img() {
    wx.downloadFile({
      url: this.data.qrcode_url,
      success: (e) => {
        // console.log(e.tempFilePath)
        this.setData({
          qrcode_img: e.tempFilePath,
        })
      }
    })
  },
  download_canvas_bg() {
    wx.downloadFile({
      url: this.data.canvas_url,
      success: (e) => {
        // console.log(e.tempFilePath)
        this.setData({
          canvas_bg: e.tempFilePath,
        })
      }
    })
  },
  //生成画板
  createCanvas() {
    let that = this;
    //开始画图
    const ctx = wx.createCanvasContext('share_canvas');
    let rpx = that.data.rpx;
    let code = that.data.code;
    // console.log(rpx)
    ctx.drawImage(that.data.canvas_bg, 0, 0, 280 * rpx, 500 * rpx);
    ctx.drawImage(that.data.qrcode_img, 105 * rpx, 350 * rpx, 70 * rpx, 70 * rpx);
    // ctx.font = 'normal bold 14px sans-serif';
    // ctx.setTextAlign('center');
    // ctx.setFillStyle('rgb(255, 255, 255)');
    // ctx.fillText('邀请码： ' + code, 140 * rpx, 462 * rpx);
    ctx.draw(true, function (e) {
      // console.log(e)
      //导出
      wx.canvasToTempFilePath({
        x: 0,
        y: 0,
        width: 280,
        height: 500,
        destWidth: 560,
        destHeight: 1000,
        canvasId: 'share_canvas',
        success: function (res) {
          // console.log(res.tempFilePath)
          that.setData({
            share_img: res.tempFilePath,
          })
          // var image_base64 = wx.getFileSystemManager().readFileSync(res.tempFilePath, "base64")
          // console.log(image_base64)
        }
      })
      wx.hideLoading();
    });


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    this.get_invite();
    this.earns();
    // this.createCanvas();
    wx.getSystemInfo({
      success: function (e) {
        const rpx = e.windowWidth / 375;
        that.setData({
          rpx: rpx,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.balance();
    this.getInfo();
    this.getRule();
    this.get_isupdate();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})